package cn.web.tunnelrobot.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 项目里程转换类
 * @Author:lichuang
 */
public class MileageConversion {
    static DecimalFormat df = new DecimalFormat("#.00");
    /**
     * 转换方法
     * 根据米转换成里程
     * @param meter
     * @return
     */
    public static String mileage(float meter){
        int D = (int)(meter / 1000);
        float d1 = (float) meter / 1000;
        BigDecimal b1 = new BigDecimal(Float.toString(d1));

        BigDecimal b2 = new BigDecimal(Float.toString(D));

        float ss = b1.subtract(b2).floatValue();
/*
        float d3=(d1 - (float) D);
*/
        /*String d2 = df.format(d1 - D) ;
        float a = Float.parseFloat(d2);*/
        float K = ss*1000;
        return "DK" + D + "+" + K;
    }
    /**
     * 转换方法
     * 根据里程转换成米
     * @param mileage
     * @return
     */
    public static int distance(String mileage){
        try {
            mileage = mileage.trim();
            String D = mileage.substring(mileage.lastIndexOf("K")+1, mileage.lastIndexOf("+"));
            String K = mileage.substring(mileage.lastIndexOf("+")+1);
            int D1 = Integer.parseInt(D)*1000;
//            int K1 = Integer.parseInt(K);
            int K1 =Double.valueOf(K).intValue();
            int distance = D1+K1;
            return distance;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 转换方法
     * 检测是否时数字型
     * @return
     */
    public static boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim()))
            return s.matches("^[0-9]*$");
        else
            return false;
    }

    public static void main(String[] args) {
        int distance = distance("DK2+100");
        System.out.println(distance);
    }
}
